/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.app;

import org.cshoo.tattoo.datacenter.api.dto.*;
import org.cshoo.tattoo.datacenter.domain.bo.LookupBo;
import org.cshoo.tattoo.datacenter.domain.po.Lookup;
import org.cshoo.tattoo.datacenter.domain.po.LookupItem;
import org.cshoo.tattoo.datacenter.domain.service.LookupService;
import org.cshoo.tattoo.embed.infra.entity.PageQueryEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
public class LookupAppService {

    @Resource
    private LookupService lookupService;

    /**
     * 校验编号是否存在
     *
     * @param code 编号
     * @return 校验结果
     */
    public boolean checkLookupCodeExists(String code) {
        return lookupService.checkLookupCodeExists(code);
    }

    /**
     * 添加lookup
     *
     * @param addLookupReq 添加lookup请求dto
     */
    public void addLookup(AddLookupReq addLookupReq) {
        Lookup lookup = new Lookup();
        BeanUtils.copyProperties(addLookupReq, lookup);
        lookupService.addLookup(lookup);
    }

    /**
     * 修改lookup
     *
     * @param updateLookupReq 修改lookup请求dto
     */
    public void updateLookup(UpdateLookupReq updateLookupReq) {
        Lookup lookup = new Lookup();
        BeanUtils.copyProperties(updateLookupReq, lookup);
        lookupService.updateLookup(lookup);
    }

    /**
     * 删除lookup
     *
     * @param id lookup id
     */
    public void deleteLookup(Long id) {
        lookupService.deleteLookup(id);
    }

    /**
     * 查询全部lookup
     *
     * @return lookup
     */
    public List<Lookup> queryLookups() {
        return lookupService.queryLookups();
    }

    /**
     * 查询lookup
     *
     * @param pageReqEntity 分页查询对象
     * @return lookup
     */
    public Page<Lookup> queryLookups(PageQueryEntity<String> pageReqEntity) {
        return lookupService.queryLookups(pageReqEntity);
    }

    /**
     * 校验lookup item编号是否存在
     *
     * @param code     编号
     * @param lookupId lookup id
     * @return 校验结果
     */
    public boolean checkLookupItemCodeExists(String code, Long lookupId) {
        return lookupService.checkLookupItemCodeExists(code, lookupId);
    }

    /**
     * 添加look up item
     *
     * @param addLookupItemReq 添加lookup item请求DTO
     * @throws Exception 异常
     */
    public void addLookupItem(AddLookupItemReq addLookupItemReq) throws Exception {
        LookupItem lookupItem = new LookupItem();
        BeanUtils.copyProperties(addLookupItemReq, lookupItem);
        lookupService.addLookupItem(lookupItem);
    }

    /**
     * 修改lookup item信息
     *
     * @param updateLookupItemReq 修改lookup item的请求信息
     */
    public void updateLookupItem(UpdateLookupItemReq updateLookupItemReq) {
        LookupItem lookupItem = new LookupItem();
        BeanUtils.copyProperties(updateLookupItemReq, lookupItem);
        lookupService.updateLookupItem(lookupItem);
    }

    /**
     * 删除lookup item
     *
     * @param id id
     */
    public void deleteLookupItem(Long id) {
        lookupService.deleteLookupItem(id);
    }

    /**
     * 查询lookup item
     *
     * @param lookupId lookup id
     * @return lookup item
     */
    public List<LookupItem> queryLookupItems(Long lookupId) {
        return lookupService.queryLookupItems(lookupId);
    }

    /**
     * 查询lookup item
     *
     * @param pageReqEntity 分页查询对象
     * @return lookup item
     */
    public Page<LookupItem> queryLookupItems(PageQueryEntity<Long> pageReqEntity) {
        return lookupService.queryLookupItems(pageReqEntity);
    }

    /**
     * 查询单个lookup用于消费侧
     *
     * @param code 编号
     * @return lookup
     */
    public QuerySingleLookupResp querySingleLookup(String code) throws Exception {
        QuerySingleLookupResp result = new QuerySingleLookupResp();
        LookupBo lookupBo = lookupService.querySingleLookup(code);
        BeanUtils.copyProperties(lookupBo, result);
        List<QuerySingleLookupItemResp> lookupItems = new ArrayList<>();
        lookupBo.getLookupItems().forEach(item -> {
            QuerySingleLookupItemResp lookupItemResp = new QuerySingleLookupItemResp();
            BeanUtils.copyProperties(item, lookupItemResp);
            lookupItems.add(lookupItemResp);
        });
        result.setLookupItems(lookupItems);
        return result;
    }
}
